<script setup>
import { ref, provide, onMounted, watch } from "vue";
import { storeToRefs } from "pinia";
import Header from "./components/header.vue";
import TxtEditor from "./components/txtEditor.vue";
import EventBus from "./common/EventBus";
import Popovers from "./components/popovers.vue";
import { createTOCView } from "./libs/ui/tree.js";
import { useAppStore } from "./store/appStore";
import { useBookStore } from "./store/bookStore";
const { ipcRenderer } = window.require("electron");

const { hideCtxMenu } = useAppStore();
const { currentHref } = storeToRefs(useAppStore());
const { curChapter, metaData, toc } = storeToRefs(useBookStore());
const { addTocByHref, findChapterByHref } = useBookStore();
let tocView;
const chapters = ref([]);

// 引用 TxtEditor 组件
const txtEditorRef = ref(null);
const metadata = ref({
  title: "",
  author: "",
  description: "",
  cover: "",
  ext: "",
  fileContent: "",
});

provide("curChapter", curChapter);
provide("chapters", chapters);
provide("metadata", metadata);

// 点击左侧菜单时更新 currentContent

const handleChapterClick = (chapter) => {
  curChapter.value = chapter;
};

onMounted(() => {
  document.addEventListener("click", (event) => {
    hideCtxMenu();
  });
});

const showContextMenu = (event, href) => {
  currentHref.value = href;
  console.log("showContextMenu", href);
  event.preventDefault();
  setTimeout(() => {
    //初始化菜单数据
    EventBus.emit("commonCtxMenu-init", 0);
    //显示菜单
    EventBus.emit("commonCtxMenu-show", event);
  }, 99);
};
//更新内容
const updateCurChapter = (href) => {
  //从数据库里面获取内容
  const chapter = ipcRenderer.sendSync(
    "db-get-chapter",
    metaData.value.bookId,
    href
  );
  //显示内容
  curChapter.value = chapter.data;
};
//重新布局目录
const updateTocView = (href) => {
  tocView = createTOCView(
    toc.value,
    (href) => {
      updateCurChapter(href);
    },
    (href, event) => {
      updateCurChapter(href);
      showContextMenu(event, href);
    }
  );
  const tocViewElement = window.$("#toc-view");
  tocViewElement.innerHTML = "";
  tocViewElement.append(tocView.element);
  tocView.setCurrentHref(href);
};
// 增加目录
const addChapter = (parentHref) => {
  const newChapter = {
    label: "未命名",
    href: `chapter-${Date.now()}`,
    subitems: null,
  };
  addTocByHref(parentHref, newChapter);
};

//增加目录
EventBus.on("addChapter", (parentHref) => {
  addChapter(parentHref);
  updateTocView();
});

//更新目录，重新载入编辑内容
EventBus.on("updateToc", (href) => {
  //更新目录
  updateTocView(href);
  //更新右边内容
  updateCurChapter(href);
});
//移除href对应的节点
EventBus.on("deleteToc", (href) => {
  const tocViewElement = window.$("#toc-view");
  // 先查找 href 对应的 a 标签节点
  const aNode = tocViewElement.querySelector(`a[href="${href}"]`);
  if (aNode) {
    // 通过 a 标签节点找到其最近的 li 父节点
    const liNode = aNode.closest("li");
    if (liNode) {
      const prevHref = liNode.previousElementSibling
        ?.querySelector("a")
        ?.getAttribute("href");
      if (prevHref) {
        updateCurChapter(prevHref);
      } else {
        //找父节点的兄弟节点的a
        const parentLiNode = liNode.parentElement;
        console.log("parentLiNode", parentLiNode);
        if (parentLiNode) {
          const prevHref =
            parentLiNode.previousElementSibling.getAttribute("href");
          console.log("prevHref", prevHref);
          if (prevHref) {
            updateCurChapter(prevHref);
          }
        }
      }
      liNode.remove();
    }
  }
});
</script>

<template>
  <Popovers></Popovers>
  <div class="container">
    <Header></Header>
    <div class="content">
      <div id="leftMenu">
        <div
          v-if="chapters.length > 0"
          v-for="(chapter, index) in chapters"
          :key="index"
          @click="handleChapterClick(chapter)"
        >
          {{ chapter.title }}
        </div>
        <div id="side-bar-header">
          <img id="side-bar-cover" />
          <div>
            <h1 id="side-bar-title"></h1>
            <p id="side-bar-author"></p>
          </div>
        </div>
        <div id="toc-view"></div>
      </div>
      <TxtEditor ref="txtEditorRef" />
    </div>
    <div class="footbar">
      <span v-if="curChapter?.content">当前在: {{ curChapter?.title }}</span>
    </div>
  </div>
</template>

<style>
html {
  height: 100%;
}

body {
  margin: 0 auto;
  height: 100%;
  font: menu;
  font-family: system-ui, sans-serif;
}

#side-bar-header {
  padding: 1rem;
  display: flex;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  align-items: center;
  padding: 5px;
}

#side-bar-cover {
  height: 10vh;
  min-height: 60px;
  max-height: 180px;
  border-radius: 3px;
  border: 0;
  background: lightgray;
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.1), 0 0 16px rgba(0, 0, 0, 0.1);
  margin-inline-end: 1rem;
}

#side-bar-cover:not([src]) {
  display: none;
}

#side-bar-title {
  margin: 0.5rem 0;
  font-size: inherit;
}

#side-bar-author {
  margin: 0.5rem 0;
  font-size: small;
  color: GrayText;
}

#toc-view {
  padding: 0.5rem;
  overflow-y: scroll;
}

#toc-view li,
#toc-view ol {
  margin: 0;
  padding: 0;
  list-style: none;
}

#toc-view a,
#toc-view span {
  display: block;
  border-radius: 6px;
  padding: 8px;
  margin: 2px 0;
}

#toc-view a {
  color: CanvasText;
  text-decoration: none;
}

#toc-view a:hover {
  background: #ccc;
}

#toc-view span {
  color: GrayText;
}

#toc-view svg {
  margin-inline-start: -24px;
  padding-inline-start: 5px;
  padding-inline-end: 6px;
  fill: CanvasText;
  cursor: default;
  transition: transform 0.2s ease;
  opacity: 0.5;
}

#toc-view svg:hover {
  opacity: 1;
}

#toc-view [aria-current] {
  font-weight: bold;
  background: #ccc;
}

#toc-view [aria-expanded="false"] svg {
  transform: rotate(-90deg);
}

#toc-view [aria-expanded="false"] + [role="group"] {
  display: none;
}

.footbar {
  height: 20px;
  width: 100%;
  background-color: #87ceeb;
  text-align: left;
  line-height: 20px;
}

.content {
  display: flex;
  flex-direction: row;
  width: 100%;
  height: calc(100vh - 120px);
  background-color: #add8e6;
  box-sizing: border-box !important;
}

#leftMenu {
  width: 200px;
  height: 100%;
  background-color: #f0f0f0;
  border-right: 1px solid #add8e6;
  overflow-y: auto;
  overflow-x: hidden;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#leftMenu div {
  padding: 0;
  cursor: pointer;
  transition: all 0.3s ease;
  text-overflow: ellipsis;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100vh;
  font-size: 12px;
}
</style>
